cc.Class({
    extends: cc.Component,

    properties: {
		
		audioBg: {
            type: cc.AudioClip,
            default: []
        },

        playerFace:{
            type:cc.Sprite,
            default:null
        },

        playerFaceNode:{
            type:cc.Node,
            default:null
        },

        levelFightBar:{
            type:cc.ProgressBar,
            default:null
        },

        fightTitleLabel:{
            type:cc.Label,
            default:null,
        },

        levelBrightBar:{
            type:cc.ProgressBar,
            default:null
        },

        brightTitleLabel:{
            type:cc.Label,
            default:null,
        },

        levelDarkFightBar:{
            type:cc.ProgressBar,
            default:null
        },

        darkTitleLabel:{
            type:cc.Label,
            default:null,
        },

        faceNode:{
            type:cc.Node,
            default:null,
        },

        hdlevelLabel:{
            type:cc.Label,
            default:null,
        },

        hidelevelLabel:{
            type:cc.Label,
            default:null,
        },

        goldLabel:{
            type:cc.Label,
            default:null,
        },

        positionLabel:{
            type:cc.Label,
            default:null,
        },
		
		hardLabel:{
            type:cc.Label,
            default:null,
        },

        taskListNode:{
            type:cc.Node,
            default:null
        },

        helpBtn:{
            type:cc.Node,
            default:null
        }
    },

    onLoad: function () {

        _TIPS.setNode(this.node);
        _Ep.setNode(this.node,1);
        _MGD.setParentNode(this.node);
        _MJOB.setParentNode(this.node,true);

        //window.fightData.autofight = false;

        window._MAP= this;

        this.node.getChildByName('mapmask').active = true;

        this.playerNode = null;

        this.FirendNodeArr = null;

        this.isMoving = false;

        this.movePath = null;

        this.mapName = _MAPCONFIG[gameData.defaultMap].name?
                        _MAPCONFIG[gameData.defaultMap].name:
                        gameData.defaultMap;

        this.historyPath = [];

        this.moveIndex = 0;

        //this.moveTime = _GAMEDATAHELPER.hasTeamSkill('jixingjun')?0.1:0.2;//移动一个单元格的时间
        this.moveTime = 0.1;

        this.ishide = 0;

        this.moveNextPoint = null;

        this.testMapPath = "map/"+_MAPCONFIG[gameData.defaultMap].path;

        this.isCity = _MAPCONFIG[gameData.defaultMap]
                      &&_MAPCONFIG[gameData.defaultMap].parent;

        this.clickPointNode = this.node.getChildByName('clickPoint');

        this.clickPointNode.active = false;

        this.jumpMap = {};

        this.npcMap = {};

        this.boxMap = {};

        this.bossMap = {};

        this.chapterMap = {};

        this.randomBossMap = {};

        this.normalNpcMap = {};

        this.areaMapType = {};//地形集合

        this.fightAreaMapType = {};//战斗地形集合

        this.areaMapArmy = {};//野区集合

        this.moveStep = 0;//移动步数

        //清除召唤物
        this.clearZhaohuan();

        this.recoverAllPlayer();

        this.initAnimation();

        this.initButton();

        this.loadMap();

        _MK.initlisten(this);

    },


    refreshMapTeam:function(){

        this.historyPath = [];
        var sourcePlayerPos = this.playerNode.getPosition();
        _GRM.putNode(this.playerNode);
        for(var i=0;i<this.FirendNodeArr.length;i++){
            _GRM.putNode(this.FirendNodeArr[i]);
        }
        //_GRM.getBackNode(this.tiledMap.node.getChildByName('playerLabel'));
        this.initAnimation();
        this.setPlayerInitPosition(sourcePlayerPos);
        this.initBeginAni();
    },


    initTestMapButton:function(){
        this.node.getChildByName('testmap')
            .getChildByName('label')
            .getChildByName('jumpbtn')
            .on(cc.Node.EventType.TOUCH_START,function(){
                var mapname = this.node.getChildByName("testmap").getChildByName("label")
                .getChildByName("map").getComponent(cc.EditBox).string;
                var target = this.node.getChildByName("testmap").getChildByName("label")
                .getChildByName("target").getComponent(cc.EditBox).string;

                if(mapname!=null&&target!=null&&_MAPCONFIG[mapname])
                    this.jumpToMap(mapname,target);
            },
            this);
    },

    refreshOneJiacengUi:function(node,index){

        var processNode = node.getChildByName('progressBar');
        var processLabel = processNode.getChildByName('label');
        var zuanNode = node.getChildByName('zuan');
        var explainNode = node.getChildByName('explain');
        var nmbNode = node.getChildByName('surplus');
        var sign = 'exp';
        var signstr = '经验掉落:';
        var maxnmb = 20;
        var leftnmb = 0;
        
        if(index==1){
            sign = 'gold';
            signstr = '金钱掉落:';
        }else if(index==2){
            sign = 'drop';
            signstr = '道具掉落:';
        }else if(index==3){
            sign = 'missFight';
            signstr = '避免遇敌';
            maxnmb=500;
        }
        
        var effectObj = _GAMEDATAHELPER.getEffectObj(sign);
        if(effectObj){
            if(sign!='missFight'){
                signstr+=(effectObj.double*100)+"%";
            }
            leftnmb = effectObj.nmb;
        }else{
            signstr+="--";
        }

        var isopen = _GAMEDATAHELPER.isEffectOpen(sign);
        var signnmb = _GAMEDATAHELPER.getEffectItemNmb(sign);
        explainNode.getComponent(cc.Label).string = signstr;
        nmbNode.getComponent(cc.Label).string = signnmb;
        processLabel.getComponent(cc.Label).string = leftnmb;
        processNode.getComponent(cc.ProgressBar).progress = leftnmb/maxnmb;
        if(isopen){
            zuanNode.active = true;
        }else{
            zuanNode.active = false;
        }
    },

    showRepfLabel:function(){
        _PFM._ROLEINDEX = 0;
        this.node.getChildByName('repfLabel').active = true;
        this.refrePfLabel();
        this.refreshPfRole();
    },

    refrePfLabel:function(){
        this.repfObj = _PFM.getPf();
        var touxiang =this.node.getChildByName('repfLabel')
        .getChildByName('kuan').getChildByName('touxian');
        var xingxiang =this.node.getChildByName('repfLabel')
        .getChildByName('kuan').getChildByName('xingxiang');

        touxiang.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(this.repfObj.face);
        _CU.initAllAniNode(this.repfObj.pf, xingxiang);

    },

    refreshPfRole:function(){

        var repfRoleList = this.node.getChildByName('repfLabel')
        .getChildByName('roleList').getChildren();

        for(var i=0;i<repfRoleList.length;i++){

            if(window.gameData.playerTeam[i]){
                repfRoleList[i].active = true;
                repfRoleList[i].getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(window.gameData.playerTeam[i].face);
            }else{
                repfRoleList[i].active = false;
            }

            if(_PFM._ROLEINDEX==i){
                repfRoleList[i].getChildByName('selected').active = true;
            }else{
                repfRoleList[i].getChildByName('selected').active = false;
            }

        }

    },


    switchMenu:function(){

        var menubtns = this.node.getChildByName('controllerMenu').getChildren();
        for(var i=0;i<menubtns.length;i++){
            var btn = menubtns[i];
            if(btn.getName()!='btnMenu'){
                btn.active = !btn.active;
            }
        }

    },

    initControllerButton:function(){

        this.helpBtn.on(cc.Node.EventType.TOUCH_START,function(){
            _MSHE.showUi(_MAP.node);
        },this.helpBtn);

        var reNameRoleList = this.node.getChildByName('renameLabel')
        .getChildByName('roleList').getChildren();
        for(var i=0;i<reNameRoleList.length;i++){
            reNameRoleList[i].roleIndex = i;
            reNameRoleList[i].on(cc.Node.EventType.TOUCH_START,function(){
                    _PFM._ROLEINDEX = this.roleIndex;
                    _MAP.showRename();
             },reNameRoleList[i]);
        }

        var repfConfirmBtn =this.node.getChildByName('repfLabel')
        .getChildByName('kuan').getChildByName('btnConfirm');
        var repfLeftBtn =this.node.getChildByName('repfLabel')
        .getChildByName('kuan').getChildByName('btnLeft');
        var repfRightBtn =this.node.getChildByName('repfLabel')
        .getChildByName('kuan').getChildByName('btnRight');
        var repfRoleList = this.node.getChildByName('repfLabel')
        .getChildByName('roleList').getChildren();
        var repfmask = this.node.getChildByName('repfLabel')
        .getChildByName('mask');

        for(var i=0;i<repfRoleList.length;i++){
            repfRoleList[i].roleIndex = i;
            repfRoleList[i].on(cc.Node.EventType.TOUCH_START,function(){
                    _PFM._ROLEINDEX = this.roleIndex;
                    //console.log('选择:',this.roleIndex,window.gameData.playerTeam[this.roleIndex]);
                    _MAP.refreshPfRole();
             },repfRoleList[i]);
        }

        repfmask.on(cc.Node.EventType.TOUCH_START,function(){
            this.node.getChildByName('repfLabel').active = false;
         },this);
        repfConfirmBtn.on(cc.Node.EventType.TOUCH_START,function(){
            //this.node.getChildByName('repfLabel').active = false;
            this.repfObj = _PFM.getPf();
            window.gameData.playerTeam[_PFM._ROLEINDEX].pf = this.repfObj.pf;
            window.gameData.playerTeam[_PFM._ROLEINDEX].face = this.repfObj.face;
            _TIPS.showMessage(window.gameData.playerTeam[_PFM._ROLEINDEX].name+"改变了形象(切换场景后刷新)");
            //_MAP.refreshPfRole();
            this.node.getChildByName('repfLabel').active = false;
         },this);
         repfLeftBtn.on(cc.Node.EventType.TOUCH_START,function(){
            console.log('left');
            _PFM.turnPage(-1);
            this.refrePfLabel();
         },this);
         repfRightBtn.on(cc.Node.EventType.TOUCH_START,function(){
            console.log('right');
            _PFM.turnPage(1);
            this.refrePfLabel();
         },this);


        var renameConfirmBtn = this.node.getChildByName('renameLabel')
                                .getChildByName('kuan').getChildByName('btns')
                                .getChildByName('confirm');
        renameConfirmBtn.on(cc.Node.EventType.TOUCH_START,function(){
           this.renameMainRole();
        },this);

        var confirmHideStoryBtn = this.node.getChildByName('storyTips')
                                    .getChildByName('btnConfirm');
        var cancelHideStoryBtn = this.node.getChildByName('storyTips')
                                    .getChildByName('btnCancel');

        cancelHideStoryBtn.on(cc.Node.EventType.TOUCH_START,function(){
                                    _MAP.node.getChildByName('storyTips').active = false;
                                },this);
        
        confirmHideStoryBtn.on(cc.Node.EventType.TOUCH_START,function(){
                                    _MAP.node.getChildByName('storyTips').active = false;
                                    _HSTORYHELPER.createHideStoryFight(_MAP.hideStoryName);
                                },this);

        _MSD.initUi(this.node.getChildByName('savelabel'),true);

        //菜单按钮
        var btnMenu = this.node.getChildByName('controllerMenu').getChildByName('btnMenu');
        btnMenu.on(cc.Node.EventType.TOUCH_START,function(){
            this.switchMenu();
        },
        this);

        var btnGoods = this.node.getChildByName('controllerMenu').getChildByName('btnGoods');
        btnGoods.on(cc.Node.EventType.TOUCH_START,function(){
            _MG.initUi(this.node,{sence:_STYPE._GOODSSENCE});
        },
        this);

        var btnSave = this.node.getChildByName('controllerMenu').getChildByName('btnSave');
        btnSave.on(cc.Node.EventType.TOUCH_START,function(){
            this.node.getChildByName('savelabel').active = true;
        },
        this);

        var btnTeam = this.node.getChildByName('controllerMenu').getChildByName('btnTeam');
        btnTeam.on(cc.Node.EventType.TOUCH_START,function(){
            _MPEOPLE.showPeople(this.node,null);
        },
        this);

        var btnShuoming = this.node.getChildByName('controllerMenu').getChildByName('btnShuoming');
        btnShuoming.on(cc.Node.EventType.TOUCH_START,function(){
            _ME.showMe();
        },
        this);

        var btnTj = this.node.getChildByName('controllerMenu').getChildByName('btnTj');
        btnTj.on(cc.Node.EventType.TOUCH_START,function(){
            _MANAGERTJ.showTJ();
        },
        this);

        var btnFuben = this.node.getChildByName('controllerMenu').getChildByName('btnFuben');
        btnFuben.on(cc.Node.EventType.TOUCH_START,function(){
            _CHAPTER.showUi(this.node);
        },
        this);

        var btnAudio = this.node.getChildByName('controllerMenu').getChildByName('btnAudio');
        btnAudio.on(cc.Node.EventType.TOUCH_START,function(){
            if(window.gameData){
                window.gameData.isPlayAudio = !window.gameData.isPlayAudio;
                this.initBgm();
            }
        },
        this);

        var btnRenwu = this.node.getChildByName('controllerMenu').getChildByName('btnRenwu');
        btnRenwu.on(cc.Node.EventType.TOUCH_START,function(){
            //_MWORK.showWork(this.node,null);
            //_GAMEDATAHELPER.openAllNpc();
            //_TIPS.showMessage('图鉴全开');
            _MCJ.showWork(this.node);
        },
        this);

        var btnJifen = this.node.getChildByName('controllerMenu').getChildByName('btnJifen');
        btnJifen.on(cc.Node.EventType.TOUCH_START,function(){
            //this.node.getChildByName('jifenLabel').active = true;
            //_HTTP.getJf();
            //this.node.getChildByName('jifenLabel').getChildByName(_QD.getcodeNodeName()).active = true;
            _CHESSJFUI.showJFUI(_MAP.node)
        },
        this);
        if(!_QD.needJf()){
            btnJifen.active = false;
        }
        //_MANAGERJF.initJf(this.node.getChildByName('jifenLabel'));

        this.node.getChildByName('renameLabel').getChildByName('mask').on(cc.Node.EventType.TOUCH_START,function(){
           this.closeRename();
        },this);
    },

    showArmy:function(){

        //_TIPS.showMessage('形势暂无',0);
        var playerMapPosition = this.getPlayerCurrentMapPosition();
        var pos = cc.v2(playerMapPosition.x,playerMapPosition.y);
        var armyArea = _MAA.getCurrentAreaArmy(pos);
        var areaType = this.getCurrentAreaType(pos);
        //console.log(_FIGHTPREPARE.getFightIntro(armyArea,areaType));
        _TIPS.showMessage(_FIGHTPREPARE.getFightIntro(armyArea,areaType));

    },

    useXj:function(){

        var goodsIndex = _GAMEDATAHELPER.getUnlockGoodsIndex(window.gameData.fightItem,"陷阱");
            if(_GAMEDATAHELPER.reduceGoods(window.gameData.fightItem,goodsIndex,1)){
                var areaConf = _MAP.getCurrentFightInfo();
                if(!_FIGHTPREPARE.initFight(areaConf.armyArea,areaConf.areaType,"animalFight")){
                    _GAMEDATAHELPER.pushItem(_IOF.createObjByName('陷阱',null))
                    _TIPS.showMessage('未发现可以对战的敌人!(五行怪物只会出现在对应的地形上)',0);
                }
            }else{
                _TIPS.showMessage("陷阱不足或者处于上锁状态");
            }

    },

    useZs:function(){

        var goodsIndex = _GAMEDATAHELPER.getUnlockGoodsIndex(window.gameData.fightItem,"战书");
            if(_GAMEDATAHELPER.reduceGoods(window.gameData.fightItem,goodsIndex,1)){
                var areaConf = _MAP.getCurrentFightInfo();
                if(!_FIGHTPREPARE.initFight(areaConf.armyArea,areaConf.areaType,"peopleFight")){
                    _GAMEDATAHELPER.pushItem(_IOF.createObjByName('战书',null))
                    _TIPS.showMessage('未发现可以对战的敌人!',0);
                }
            }else{
                _TIPS.showMessage("战书不足或者处于上锁状态");
            }

    },

    initButton:function(){
        _MXYT.initButton(this.taskListNode);
        _MXYT.refreshUi(this.taskListNode);
        this.initTestMapButton();
        this.initControllerButton();
    },

    playBgm:function(bgmname,audioBgArr){
		if(audioBgArr&&audioBgArr.length>0){
            for(var i=0;i<audioBgArr.length;i++){
                
                if(audioBgArr[i].name == bgmname){
                    
                    cc.audioEngine.play(audioBgArr[i], true, 1);
                    
                    break;
                    
                }
                
            }
        }
	},

    initAnimation:function(){
        this.playerNode = _GRM.createPlayerNode(window.gameData.playerTeam[0].pf,null);
        this.FirendNodeArr = [];
        for(var i=1;i<window.gameData.playerTeam.length;i++){
            this.FirendNodeArr.push(_GRM.createPlayerNode(window.gameData.playerTeam[i].pf,null));
        }
    },

    realMapMove:function(target,param){

        var targetPosition=param.t;
        var movedir=param.m;
        var tpoint = param.p;

        this.playerNode.runAction(cc.moveTo(this.moveTime,targetPosition));

        this.historyPath.unshift({point:targetPosition,dir:movedir,p:tpoint});
        if(this.historyPath.length>5)
            this.historyPath.length=5;

        for(var i=0;i<this.FirendNodeArr.length;i++){
            if(this.historyPath[i+1]){
                this.FirendNodeArr[i].runAction(cc.moveTo(this.moveTime,this.historyPath[i+1].point));

                this.switchWalkAni(this.FirendNodeArr[i],this.historyPath[i+1].p);

                if(this.historyPath[i+1].dir==3){
                    this.FirendNodeArr[i].setScale(_CS.playerScale,_CS.playerScale);
                }else if(this.historyPath[i+1].dir==4){
                    this.FirendNodeArr[i].setScale(-_CS.playerScale,_CS.playerScale);
                }
            }
        }

        var mx = -targetPosition.x;
        var my = -targetPosition.y;

        var mapMovePos = this.isMapMove(mx,my,movedir);
        this.tiledMap.node.runAction(cc.moveTo(this.moveTime,cc.v2(mapMovePos.x,mapMovePos.y)));
        
    },

    turnMapToPlayer:function(){

        var playerPosition = this.playerNode;
        var mapPos = this.isMapMove(-playerPosition.x,-playerPosition.y,null);
        this.tiledMap.node.setPosition(cc.v2(mapPos.x,mapPos.y));
    },

    isMapMove:function(tx,ty,movedir){

        if(tx<this.leftx){
            tx = this.leftx;
        }else if(tx>this.rightx){
            tx=this.rightx
        }

        if(ty<this.bomy){
           ty=this.bomy;
        }else if(ty>this.topy){
            ty = this.topy;
        }

        return {x:tx,y:ty};

        
    },

    canJumpToMap:function(map,target){

        if(!_MAPCONFIG[map]){
            _TIPS.showMessage('此处未配置');
            return false;
        }

        if(_GAMEDATAHELPER.isArmy()
            &&_MAPCONFIG[map].isRoot
            &&!_GAMEDATAHELPER.isChapterOpen(map)
        ){
            if(_MAPCONFIG[map].premap){
                _TIPS.showMessage('解锁'+_MAPCONFIG[map].premap.join(',')+'即可以解锁'+map);
            }else{
                _TIPS.showMessage('尚未解锁该地图');
            }
            return false;
        }

        if(
            window.gameData.cityLog[map]
            &&window.gameData.country!=null
            &&window.gameData.country!=window.gameData.cityLog[map]
        ){
            //this.isMoving = false;
            _MANAGERFIGHTCITY.showSelect(_MAP.node,map,target,null);
            return false;
        }else{
            return true;
        }
    },

    loadHideStory:function(){

        var storytalk = _HSTORYHELPER.getHideStory();
        if(storytalk!=null){
            _MANAGERTALK.showTalk(this.node,storytalk,_MAP);
            return true;
        }

        return false;
    },

    showHideStory(storyname){

        this.hideStoryName = storyname;
        this.node.getChildByName('storyTips').active = true;
        this.node.getChildByName('storyTips')
        .getChildByName('message').getComponent(cc.Label).string = "确定要触发剧情:"+storyname;

    },

    //加载剧情对话
    showAutoStory:function(){
        
        if(!window.gameData.gameStart){
            window.gameData.gameStart = 1;
            var talkObj = _TALKHELPER.createGameStartTalk(); 
            _MANAGERTALK.showTalk(this.node,talkObj,_MAP);

        }else if(
            window.gameData.fsbpass
            &&!window.gameData.gameEnd
        ){
            window.gameData.gameEnd = 1;
            var talkObj = _TALKHELPER.createGameEndTalk(); 
            _MANAGERTALK.showTalk(this.node,talkObj,_MAP);
        }
        
    },

    jumpToGameEnd:function(){
        this.giveBackAll();
        cc.director.loadScene('gameEnd');
    },

    jumpToMap:function(map,target){

        //解锁地图
        _GAMEDATAHELPER.openChapter(map);

        if(this.saveDataEnable()){
            _MSD.autoSave();
        }
            
        if(target!=null){//非退出跳转 记录当前跳转前坐标
            //console.log('覆盖坐标');
            window.gameData.lastjumpx=this.playerNode.getPosition().x;
            window.gameData.lastjumpy=this.playerNode.getPosition().y;
        }else{//退出跳转
            window.gameData.lastx=window.gameData.lastjumpx;
            window.gameData.lasty=window.gameData.lastjumpy;
        }

        this.giveBackAll();
        window.gameData.lastmap = gameData.defaultMap;
        window.gameData.defaultMap = map;
        window.gameData.targetPoint = target;
        window._JUMPSENCE = 'map';
        cc.director.loadScene('jump');
    },

    getFightAreaName:function(){
        var playerMapPosition = _MAP.getPlayerCurrentMapPosition();
        var pos = cc.v2(playerMapPosition.x,playerMapPosition.y);
        for(var key in _MAP.fightAreaMapType){
            var layer = _MAP.fightAreaMapType[key];   
            if(layer.getTileGIDAt(pos)){
                return key ;
            }   
        }
        return null;
    },

    jumpToFight:function(){

        //_FIGHTHELPER.reset();
        window.gameData.lastx=this.playerNode.getPosition().x;
        window.gameData.lasty=this.playerNode.getPosition().y;

        if(this.saveDataEnable()){
            _MSD.autoSave();
        }

        this.giveBackAll();
        window._JUMPSENCE = 'fight';
        cc.director.loadScene('jump');
    },
	
	jumpToFightNoAutoSave:function(){
		window.gameData.lastx=this.playerNode.getPosition().x;
        window.gameData.lasty=this.playerNode.getPosition().y;
        this.giveBackAll();
        window._JUMPSENCE = 'fight';
        cc.director.loadScene('jump');
	},

    giveBackAll:function(){
        _GRM.getBackNode(this.tiledMap.node.getChildByName('playerLabel'));
        _GRM.getBackNode(this.node);
    },

    //移动是否停止
    moveEnd:function(){

        //this.isMoving = false; 可继续移动

        var playerMapPosition = this.getPlayerCurrentMapPosition();
        var positionKey = playerMapPosition.x+"-"+playerMapPosition.y;
        var jumpMapObj = this.jumpMap[positionKey];
        if(jumpMapObj){
            if(jumpMapObj.isOut||this.canJumpToMap(jumpMapObj.map,jumpMapObj.target)){
                this.jumpToMap(jumpMapObj.map,jumpMapObj.target);
            }else{
                this.isMoving = false;
            }
            return true;
        }

        if(_GAMEDATAHELPER.isArmy()
            &&!this.ishide
            &&_MAA.touchArmy(cc.v2(playerMapPosition.x,playerMapPosition.y))){
            return true;
        }

        return false;
    },


    jumpToFinalBoss:function(){

        if(window.gameData.workList&&window.gameData.workList.length>0){
            _TIPS.showMessage("主线任务未完成无法挑战最终BOSS！");
            return true;
        }

        _MSD.autoSave();
        
        window.fightData = {
            playerTeam:[],
            chapter:_MCH.getHardName(),
            isFightBoss:false,
            isFinal:true,
        };

        for(var i=0;i<window.gameData.playerTeam.length;i++){
            window.fightData.playerTeam[i]=window.gameData.playerTeam[i];
            window.fightData.playerTeam[i].isplayer = true;
            var playerData = _ROLEDATA.getRoleDataOfPlayer(window.fightData.playerTeam[i]);
            window.fightData.playerTeam[i].hp = playerData.maxHp;
            window.fightData.playerTeam[i].mg = playerData.maxMagic;
            window.fightData.playerTeam[i].chessStatus = null;
        }

        this.giveBackAll();

        window._JUMPSENCE = 'chessFight'
        cc.director.loadScene('jump');

        return true;

    },

    jumpToFightBoss:function(boss){

        _MSD.autoSave();

        window.fightData = {
            playerTeam:[],
            boss:boss,
            chapter:this.mapName
        };

        for(var i=0;i<window.gameData.playerTeam.length;i++){
            window.fightData.playerTeam[i]=window.gameData.playerTeam[i];
            window.fightData.playerTeam[i].isplayer = true;
            var playerData = _ROLEDATA.getRoleDataOfPlayer(window.fightData.playerTeam[i]);
            window.fightData.playerTeam[i].hp = playerData.maxHp;
            window.fightData.playerTeam[i].mg = playerData.maxMagic;
            window.fightData.playerTeam[i].chessStatus = null;
        }

        this.giveBackAll();

        window._JUMPSENCE = 'chessFight'
        cc.director.loadScene('jump');

        return true;

    },

    jumpToChapter:function(chapter,areaType,level){

        if(!_NPCMAP[chapter]){
            _TIPS.showMessage(chapter+"尚未配置");
            return false;
        }

        _MSD.autoSave();

        window.gameData.lastx=this.playerNode.getPosition().x;
        window.gameData.lasty=this.playerNode.getPosition().y;

        window.fightData = {
            playerTeam:[],
            chapter:chapter,
            level:level,
            isFightBoss:false,
            isFinal:false,
            areaType:areaType
        };

        for(var i=0;i<window.gameData.playerTeam.length;i++){
            window.fightData.playerTeam[i]=window.gameData.playerTeam[i];
            window.fightData.playerTeam[i].isplayer = true;
            var playerData = _ROLEDATA.getRoleDataOfPlayer(window.fightData.playerTeam[i]);
            window.fightData.playerTeam[i].hp = playerData.maxHp;
            window.fightData.playerTeam[i].mg = playerData.maxMagic;
            window.fightData.playerTeam[i].chessStatus = null;
        }

        this.giveBackAll();

        window._JUMPSENCE = 'chessFight'
        cc.director.loadScene('jump');

        return true;
    },

    //挑战模式
    jumpToTz:function(tzchapter,playerTeam){

        _MSD.autoSave();

        window.gameData.lastx=this.playerNode.getPosition().x;
        window.gameData.lasty=this.playerNode.getPosition().y;

        window.fightData = {
            playerTeam:[],
            chapter:null,
            level:0,
            isFightBoss:false,
            isFinal:false,
            areaType:null,
            tzChapter:tzchapter
        };

        /*
        for(var i=0;i<playerTeam.length;i++){
            window.fightData.playerTeam[i]=playerTeam[i];
            window.fightData.playerTeam[i].isplayer = true;
            var playerData = _ROLEDATA.getRoleDataOfPlayer(window.fightData.playerTeam[i]);
            window.fightData.playerTeam[i].hp = playerData.maxHp;
            window.fightData.playerTeam[i].mg = playerData.maxMagic;
            window.fightData.playerTeam[i].chessStatus = null;
        }*/

        this.giveBackAll();

        window._JUMPSENCE = 'chessFight'
        cc.director.loadScene('jump');

        return true;
    },

    //恢复玩家状态
    recoverAllPlayer:function(){

        for(var i=0;i<window.gameData.playerTeam.length;i++){
            window.gameData.playerTeam[i].isplayer = true;
            window.gameData.playerTeam[i].chessStatus = null;
            var playerData = _ROLEDATA.getRoleDataOfPlayer(window.gameData.playerTeam[i]);
            window.gameData.playerTeam[i].hp = playerData.maxHp;
            window.gameData.playerTeam[i].mg = playerData.maxMagic;
        }

    },


    getRewardFromBox:function(randomType){

        

        var lv =_NPCMAP[window.gameData.defaultMap]?
                _NPCMAP[window.gameData.defaultMap].lv 
                :10;
        
        var boxper = _GAMEDATAHELPER.getBoxNmbBySelect()+1;

        //var randomType = Math.random();
        var message = "(lv."+lv+")";

        if(randomType=="exp"){
            var exp = 100*boxper;
            for(var i=0;i<window.gameData.playerTeam.length;i++){
                //window.gameData.playerTeam[i].exp+=exp;
                _MCRD.getRewardExp(window.gameData.playerTeam[i],null,exp,null);
            }
            return message+"经验"+exp;
        }else if(randomType=="gold"){
            var gold = Math.floor(10000*lv*(Math.random()*0.5+0.5))*boxper;
            _GAMEDATAHELPER.oprMoney(gold);
            return message+"钱"+gold;
        }else if(randomType=="item"){
            var randomItem = _FIGHTITEMHELPER.randomItemByMapLevel(lv,'attr');
            if(randomItem){
                var tempitemobj = _IOF.createObjByName(randomItem,null);
                tempitemobj.nmb = boxper;
                _GAMEDATAHELPER.pushItem(tempitemobj);
                return message+randomItem+"*"+boxper;
            }
            return message+"啥也没得到...";
        }else if(randomType=="anqi"){
            var randomItem = _FIGHTITEMHELPER.randomItemByMapLevel(lv,'attack');
            if(randomItem){
                var tempitemobj = _IOF.createObjByName(randomItem,null);
                tempitemobj.nmb = boxper;
                _GAMEDATAHELPER.pushItem(tempitemobj);
                return message+randomItem+"*"+boxper;
            }
            return message+"啥也没得到...";
        }else if(randomType=="spe"){
            var randomFw = _FWJPHELPER.getFwBox(window.gameData.defaultMap);
            if(randomFw&&_FIGHTITEM[randomFw]){
                var tempitemobj = _IOF.createObjByName(randomFw,null);
                tempitemobj.nmb = boxper;
                _GAMEDATAHELPER.pushItem(tempitemobj);
                return message+randomFw+"*"+boxper;
            }
            return message+"啥也没得到...";
        }else{
            return message+"啥也没得到...";
        }

    },

    //直接弹出NPC商店
    showNpcShop:function(npc){

        var shopConf = _SHOP[npc.t];

        if(shopConf){
            if(shopConf.type=="jg"){
                _MJG.showJG(this.node);
            }else if(shopConf.type=="rh"){
                _RHM.showUi(this.node,null);
            }else if(shopConf.type=="dz"){
                _MAP.showDz();
            }else if(shopConf.type=="cjb"){
                _MAP.showCJB();
            }else if(shopConf.type=="pf"){
                _MAP.showRepfLabel();
            }else if(shopConf.type=="wood"){
                _WOODSHOP.showWoodShop(this.node);
            }else if(shopConf.type=="bossjy"){
                _BOSSLABEL.showBossLabel(this.node);
            }else if(shopConf.type=="saveTeam"){
                _MSAVEPEOPLE.showPeople(_MAP.node);
            }else if(shopConf.type=="tz"){
                _MTZ.showUi(_MAP.node);
            }else if(shopConf.type=="xq"){
                _MAP.showXQ();
            }else if(shopConf.type=="jgshop"){
                _JGSHOP.showJG(_MAP.node,shopConf.race);
            }else{
                _MG.initUi(this.node,{sence:_STYPE._SHOPSENCE,npc:npc});
            }
            return true;
        }

        return false;

    },


    touchNext:function(tpoint){

        if(this.npcMap[tpoint[0]+'-'+tpoint[1]]){

            //_MANAGERROLELABEL.showLabel(this.npcMap[tpoint[0]+'-'+tpoint[1]],this.node);
            var npcobj = this.npcMap[tpoint[0]+'-'+tpoint[1]];
            var npcname = npcobj.t;
            if(!this.showNpcShop(npcobj)){
                if(_NORMALNPC[npcname]){
                    _NORMALNPTALKHELPER.showNpcTalk(npcname);
                }else if(_MANAGERTJ.isTjNpc(npcname)){
                    _MANAGERTJ.showTjNpcTalk(npcname);
                }
            }

            return true;

        }else if(this.bossMap[tpoint[0]+'-'+tpoint[1]]){

            //剧情战斗
            this.jumpToFightBoss(this.bossMap[tpoint[0]+'-'+tpoint[1]]);
            return true;
           
        }else if(this.randomBossMap[tpoint[0]+'-'+tpoint[1]]){

            _MANAGERTALK.jumpToFightOfType([window.gameData.playerTeam[0]],
                                            this.randomBossMap[tpoint[0]+'-'+tpoint[1]],
                                            'normalFight');
            return true;

        }else if(this.normalNpcMap[tpoint[0]+'-'+tpoint[1]]){
            var tempnormalnpcconf = this.normalNpcMap[tpoint[0]+'-'+tpoint[1]];
            var talkObj = _TALKHELPER.createNormalNpcTalk(tempnormalnpcconf); 
            _MANAGERTALK.showTalk(this.node,talkObj,_MAP);
            return true;
        }else if(this.boxMap[tpoint[0]+'-'+tpoint[1]]){
            _GAMEDATAHELPER.logBox(window.gameData.defaultMap,tpoint[0],tpoint[1]);
            _GRM.putNode(this.boxMap[tpoint[0]+'-'+tpoint[1]].node);
            var message = this.getRewardFromBox(this.boxMap[tpoint[0]+'-'+tpoint[1]].type);
            //this.refreshRoleInfo();
            _TIPS.showMessage('开启宝箱:'+message,0);
            this.boxMap[tpoint[0]+'-'+tpoint[1]] = null;
            return true;

        }else if(this.chapterMap[tpoint[0]+'-'+tpoint[1]]){

            if(!_GAMEDATAHELPER.isChapterOpen(this.chapterMap[tpoint[0]+'-'+tpoint[1]])){

                var mapname = this.chapterMap[tpoint[0]+'-'+tpoint[1]];

                if(_MAPCONFIG[mapname].premap){
                    _TIPS.showMessage('解锁'+_MAPCONFIG[mapname].premap.join(',')+'即可以解锁'+mapname);
                }else{
                    _TIPS.showMessage('尚未解锁该地图');
                }

                //_TIPS.showMessage('尚未解锁'+this.chapterMap[tpoint[0]+'-'+tpoint[1]],0);
                return true;
            }

        }else{

            return false;

        }

    },


    mapmove:function(){

        if(this.movePath&&this.moveIndex<this.movePath.length){
            var tpoint = this.movePath[this.moveIndex];
            if(!this.touchNext(tpoint)){
                this.moveNextPoint = {x:tpoint[0],y:tpoint[1]};
                var movedir = this.refreshPlayerDir(tpoint);
                var targetPosition = _MANAGERMAP.getMapNodePosByTiled(tpoint[0],tpoint[1],this.maxH,this.maxW);

                this.moveIndex++;
                this.node.runAction(
                                    cc.sequence(
                                        cc.callFunc(this.realMapMove,this,{t:targetPosition,m:movedir,p:tpoint}),
                                        cc.delayTime(this.moveTime),
                                        cc.callFunc(function(){
                                            if(this.moveEnd()){
                                                return;
                                            }else{
                                                this.mapmove();
                                            }

                                        },this)
                                    )
                                );
            }else{
                this.isMoving = false;
                this.moveNextPoint = null;
            }
        }else{
            this.isMoving = false;
            this.moveNextPoint = null;

            if(_MK._Dir){
                _MK.moveByDir();
            }
            //this.jumpToFight();
        }

    },

    loadMap:function(){

        this.tiledMap = this.node.getChildByName('map').getComponent(cc.TiledMap);
		
		cc.loader.loadRes(this.testMapPath, function(err, map){

            _MAP.node.getChildByName('mapmask').active = false;

            _MAP.tiledMap.tmxAsset = map;

            //console.log('人物那层是否隐藏？',_MAP.tiledMap.node.getChildByName('playerLabel').active);
            if(
                _MAP.tiledMap.node
                &&_MAP.tiledMap.node.getChildByName('playerLabel')
            ){
                _MAP.tiledMap.node.getChildByName('playerLabel').active=true;
            }

            //清除变身效果
            for(var i=0;i<window.gameData.playerTeam.length;i++){
                window.gameData.playerTeam[i].animals = null;
            }

            _MAP.initPositionerea();

            _MAP.initJump();

            _MAP.initChapter();

            _MAP.initPlayer();

            _MAP.initBgm();

            _MAP.initNpc();

            _MAP.initBoss();

            _MAP.initBox();

            _MAA.initAreaArmy(_MAP.tiledMap);

            _MAA.initFightAreaArmy(_MAP.tiledMap);

            _MAP.initAreaMap(_MAP.tiledMap);

            _MAP.tiledMap.node.on(cc.Node.EventType.TOUCH_START, _MAP.mapClickFuc, _MAP.tiledMap.node); 

            _MAP.refreshInfoLabel();

            _MAP.refreshRoleInfo();

            _MAP.initBeginAni();

            _MAP.showAutoStory();

            if(_MAP.isCity){
                _GAMEDATAHELPER.signMap(gameData.defaultMap);
            }

            var armyInfo =_MAP.getCurrentFightInfo();
            //console.log('当前野区',armyInfo);
            if(!armyInfo.armyArea
                ||_GAMEDATAHELPER.hasTeamSkill('yechui')){
                //console.log('恢复状态');
                _GAMEDATAHELPER.backCityhp();
            }else{
                //console.log('无法恢复状态');
            }

		});
    },


    initAreaMap:function(tiledMap){
        for(var key in window._MAPTYPE){
            if(tiledMap.getLayer(key)){
                this.areaMapType[key]=tiledMap.getLayer(key);
            }
        }
    },

    getCurrentAreaType:function(pos){

        for(var key in this.areaMapType){

            var layer = this.areaMapType[key];

            if(layer.getTileGIDAt(pos)){
                return key ;
            }
            //else{
                //console.log('当前位置:',pos,layer.getTileGIDAt(pos));
            //}

        }

        return null;

    },

    initBox:function(){

        if(this.tiledMap.getObjectGroup("npc")
            &&this.tiledMap.getObjectGroup("npc").getObjects()
            &&this.tiledMap.getObjectGroup("npc").getObjects().length>0){
            var npcPoints = this.tiledMap.getObjectGroup("npc").getObjects();
            for(var i=0;i<npcPoints.length;i++){
                if(npcPoints[i].name=="图鉴"){
                    var mappos = _MANAGERMAP.getMapObjPos(npcPoints[i]);
                    if(!_GAMEDATAHELPER.isBoxLoged(window.gameData.defaultMap,mappos.x,mappos.y)){

                        var random = _FWJP[window.gameData.defaultMap]?_CU.randomObj(_CS.selectBoxNpcMap)
                                    :_CU.randomObj(_CS.boxNpcMap);

                        var playerNode = _GRM.createPlayerNode(random.npc,null);
                        var npcmappos = _MANAGERMAP.getMapNodePosByTiled(mappos.x,mappos.y,this.maxH,this.maxW);
                        this.tiledMap.node.getChildByName('playerLabel').addChild(playerNode);
                        playerNode.setPosition(npcmappos);
                        this.boxMap[mappos.x+"-"+mappos.y]={node:playerNode,type:random.type};


                    }
                }
            }
        }

    },

    getTaskConfByChapter:function(){
        var chapter = this.mapName;
        var taskConf = window.gameData.workList[0];
        if(taskConf&&!taskConf.complete&&taskConf.map==chapter){
            return taskConf;
        }
        return null;
    },

    initBoss:function(){
        var taskConf = this.getTaskConfByChapter();

        if(taskConf
            &&this.tiledMap.getObjectGroup("boss")
            &&this.tiledMap.getObjectGroup("boss").getObjects()
            &&this.tiledMap.getObjectGroup("boss").getObjects().length>0){
            
            var npcPoints = this.tiledMap.getObjectGroup("boss").getObjects()[0];
            var mappos = _MANAGERMAP.getMapObjPos(npcPoints);
            var bossname = taskConf.taskname;
            var bossconf =  _NPC[taskConf.detail[0].target];
            this.bossMap[mappos.x+"-"+mappos.y]=bossname;
            var npcmappos = _MANAGERMAP.getMapNodePosByTiled(mappos.x,mappos.y,this.maxH,this.maxW);
            var playerNode = _GRM.createPlayerNode(bossconf.pf,bossname);
            playerNode.setPosition(npcmappos);
            this.tiledMap.node.getChildByName('playerLabel').addChild(playerNode);

        }

        /*
        if(this.tiledMap.getObjectGroup("randomboss")
            &&this.tiledMap.getObjectGroup("randomboss").getObjects()
            &&this.tiledMap.getObjectGroup("randomboss").getObjects().length>0){

            //清理已经阵亡的角色
            _RBOSS.clearArr();
            
            var npcPoints = this.tiledMap.getObjectGroup("randomboss").getObjects();
            for(var i=0;i<npcPoints.length;i++){
                var mappos = _MANAGERMAP.getMapObjPos(npcPoints[i]);
                var bossname = _RBOSS.getRandomBoss(window.gameData.defaultMap);
                if(
                    bossname
                    //&&_RBOSS.isLive(bossname)
                ){
                    var bossconf =  _NPC[bossname];
                    this.randomBossMap[mappos.x+"-"+mappos.y]=bossname;
                    var npcmappos = _MANAGERMAP.getMapNodePosByTiled(mappos.x,mappos.y,this.maxH,this.maxW);
                    var playerNode = _GRM.createPlayerNode(bossconf.pf);
                    playerNode.setPosition(npcmappos);
                    this.tiledMap.node.getChildByName('playerLabel').addChild(playerNode);
                }
            }

        }*/

    },


    initNpc:function(){

        var npcArr = [];

        if(this.tiledMap.getObjectGroup("npc")
            &&this.tiledMap.getObjectGroup("npc").getObjects()
            &&this.tiledMap.getObjectGroup("npc").getObjects().length>0){
            var npcPoints = this.tiledMap.getObjectGroup("npc").getObjects();
            for(var i=0;i<npcPoints.length;i++){
                if(npcPoints[i].name&&npcPoints[i].name!='图鉴'){
                    //console.log('读取npc:',npcPoints[i].name);
                    var npct = npcPoints[i].name;
                    var mappos = _MANAGERMAP.getMapObjPos(npcPoints[i]);
                    
                    if(_TJNPC[npct]&&_GAMEDATAHELPER.hasNpcTj(npct)){
                        var npcmappos = _MANAGERMAP.getMapNodePosByTiled(mappos.x,mappos.y,this.maxH,this.maxW);
                        _GRM.createNpcSign(this.tiledMap.node.getChildByName('playerLabel'),npcmappos);
                    }

                    npcArr.push({t:npct,x:mappos.x,y:mappos.y});
                }
            }
        }

        for(var i=0;i<npcArr.length;i++){
            var npc = npcArr[i];
            if(_SHOP[npc.t]||_NORMALNPC[npc.t]||_TJNPC[npc.t]){
                this.npcMap[npc.x+"-"+npc.y]=npc;
            }
        }

    },


    removeNpc:function(npcname){

        for(var key in this.npcMap){
            var npc = this.npcMap[key];
            if(npc&&npc.t==npcname&&npc.node){
                _GRM.putNode(npc.node);
                this.npcMap[key]=null;
            }
        }

    },


    initBgm:function(){
        if(
            window.gameData
            &&window.gameData.isPlayAudio
            ){
            if(this.tiledMap.getObjectGroup("bgm")
                &&this.tiledMap.getObjectGroup("bgm").getObjects()
                &&this.tiledMap.getObjectGroup("bgm").getObjects().length>0){
                this.playBgm(this.tiledMap.getObjectGroup("bgm").getObjects()[0].name,this.audioBg);
            }else{
                this.playBgm(_CS.defaultBgm,this.audioBg);
            }
        }else{
            cc.audioEngine.stopAll();
        }

    },

    initChapter:function(){
        if(this.tiledMap.getObjectGroup("chapter")){
            var jumpPoints = this.tiledMap.getObjectGroup("chapter").getObjects();
            for(var i=0;i<jumpPoints.length;i++){
                if(jumpPoints[i].name){
                    var mappos = _MANAGERMAP.getMapObjPos(jumpPoints[i]);
                    this.chapterMap[mappos.x+"-"+mappos.y]=jumpPoints[i].name;
                }

            }
        }
    },


    initJump:function(){
        if(this.tiledMap.getObjectGroup("jump")){
            var jumpPoints = this.tiledMap.getObjectGroup("jump").getObjects();
            for(var i=0;i<jumpPoints.length;i++){

                if(jumpPoints[i].name){
                    var arr = jumpPoints[i].name.split("_");
                    var mappos = _MANAGERMAP.getMapObjPos(jumpPoints[i]);

                    if(arr.length>=2){
                        this.jumpMap[mappos.x+"-"+mappos.y]={map:arr[0],target:arr[1]};
                    }else{
                        //单向跳转 直接退出
                        this.jumpMap[mappos.x+"-"+mappos.y]={map:window.gameData.lastmap,target:null,isOut:true};
                    }
                }

            }
        }
    },

    setPlayerInitPosition(playerPos){
        this.playerNode.setPosition(playerPos);

        for(var i=0;i<this.FirendNodeArr.length;i++){
            this.FirendNodeArr[i].setPosition(playerPos);

            this.tiledMap.node.getChildByName('playerLabel').addChild(this.FirendNodeArr[i]);
        }

        this.tiledMap.node.getChildByName('playerLabel').addChild(this.playerNode);

        this.turnMapToPlayer();
    },

    initPlayer:function(){

        //先判断有没有目标坐标
        if(window.gameData.targetPoint!=null){

            if(this.tiledMap.getObjectGroup("target")){
                var targetPoints = this.tiledMap.getObjectGroup("target").getObjects();
                for(var i=0;i<targetPoints.length;i++){
                    if(targetPoints[i].name == window.gameData.targetPoint){

                        window.gameData.targetPoint = null;
                        
                        var playerPos = _MANAGERMAP.getDirectPosByRealPos(targetPoints[i].offset.x,targetPoints[i].offset.y,this.maxH,this.maxW);
                        
                        this.setPlayerInitPosition(playerPos);

                        break;
                    }
                }
                
            }else{
                this.setPlayerInitPosition(cc.v2( 0, 0));
            }

        }else if(
            window.gameData.lastx!=null
            &&window.gameData.lasty!=null
            ){
                //console.log('目标坐标不存在',window.gameData.lastx,window.gameData.lasty);
                this.setPlayerInitPosition(cc.v2( window.gameData.lastx, window.gameData.lasty));
                
        }

        
        this.node.removeChild(this.clickPointNode);
        this.tiledMap.node.addChild(this.clickPointNode);

    },

    moveToPoint:function(touchWorldPos,showblink){

        if(_MANAGERMAP.isMapPointCanTouch(_MAP.tiledMap,touchWorldPos.x,touchWorldPos.y,_MAP.maxH,_MAP.maxW)){
           
            //_MAP.tiledMap.node.setPosition(_MAP.playerNode.getPosition().x*(-1),_MAP.playerNode.getPosition().y*(-1));
            var tpoint = _MANAGERMAP.getMapPos(touchWorldPos.x,touchWorldPos.y,_MAP.maxH,_MAP.maxW);
            var spoint = _MAP.moveNextPoint?
                         _MAP.moveNextPoint
                         :_MANAGERMAP.getMapPos(_MAP.playerNode.x,_MAP.playerNode.y,_MAP.maxH,_MAP.maxW);
            //console.log('起止坐标',spoint,tpoint);
            
            _MAP.movePath = _MANAGERMAP.getMovePoint(_MAP.tiledMap,spoint.x,spoint.y,tpoint.x,tpoint.y,_MAP.maxH,_MAP.maxW);
            if(_MAP.movePath&&_MAP.movePath.length>0){
                if(showblink){
                    _MAP.blinkPoint(touchWorldPos,false);
                }
                _MAP.moveIndex = 0;
                if(!_MAP.isMoving){
                    _MAP.isMoving = true;
                    _MAP.mapmove();
                }
            }else{
                if(showblink){
                    _MAP.blinkPoint(touchWorldPos,true);
                }
            }
        }else{
            if(showblink){
                _MAP.blinkPoint(touchWorldPos,true);
            }
            //console.log('无法移动...');
        }

    },

    clearZhaohuan:function(){

        for(var i=0;i<window.gameData.playerTeam.length;){
            if(window.gameData.playerTeam[i].isZhaohuan){
                window.gameData.playerTeam.splice(i,1);
            }else{
                i++;
            }
        }

    },

    //地图点击事件
    mapClickFuc:function(event){
        //世界坐标转换为节点内坐标
        let touchWorldPos=_MAP.tiledMap.node.convertToNodeSpaceAR(event.getLocation());

        _MAP.moveToPoint(touchWorldPos,true);        

    },

    blinkPoint:function(touchWorldPos,isForbidden){
        this.clickPointNode.opacity = 150;
        if(isForbidden){
            this.clickPointNode.color = new cc.color("#FF000096");
        }else{
            this.clickPointNode.color = new cc.color("#00FF0A96");
        }
        this.clickPointNode.active =true;
        this.clickPointNode.setPosition(_MANAGERMAP.getMapNodePosByNode(touchWorldPos.x,touchWorldPos.y,this.maxH,this.maxW));
        this.clickPointNode.runAction(cc.fadeOut(0.5));
    },

    getPlayerCurrentMapPosition(){

        return _MANAGERMAP.getMapPos(this.playerNode.x,this.playerNode.y,this.maxH,this.maxW);

    },

    initBeginAni:function(){

        //console.log('初始化人的动画');

        if(this.playerNode){

            var playerMapPosition = this.getPlayerCurrentMapPosition();
            var pos = cc.v2(playerMapPosition.x,playerMapPosition.y);
            var tmpAreaType = this.getCurrentAreaType(pos);

            //console.log('初始化人的动画2',tmpAreaType);

            if(tmpAreaType=='水'||tmpAreaType=='沼泽'){
                this.playerNode.preAni = 'boatwalk';
                this.playerNode.getComponent(cc.Animation).play('boatwalk');
                for(var i=0;i<this.FirendNodeArr.length;i++){
                    this.FirendNodeArr[i].getComponent(cc.Animation).play('boatwalk');
                }
            }
        }
    },

    switchWalkAni:function(playerNode,tpoint){

        var tmpAreaType = this.getCurrentAreaType(cc.v2(tpoint[0],tpoint[1]));
        //console.log('当前地形:',tmpAreaType);
        if((tmpAreaType=='水'||tmpAreaType=='沼泽')&&playerNode.preAni!='boatwalk'){
            playerNode.preAni = 'boatwalk';
            playerNode.getComponent(cc.Animation).play('boatwalk');
        }else if((tmpAreaType!='水'&&tmpAreaType!='沼泽')&&playerNode.preAni!='walk'){
            playerNode.preAni = 'walk';
            playerNode.getComponent(cc.Animation).play('walk');
        }

    },

    //刷新角色方向动画 返回方向值 1--上  2--下  3--左 4--右
    refreshPlayerDir(tpoint){
        var spoint = this.getPlayerCurrentMapPosition();

       this.switchWalkAni(this.playerNode,tpoint);

        if(tpoint[0]>spoint.x){
            this.playerNode.setScale(-_CS.playerScale,_CS.playerScale);
            return 4;
        }else if(tpoint[0]<spoint.x){
            this.playerNode.setScale(_CS.playerScale,_CS.playerScale);
            return 3;
        }else if(tpoint[1]>spoint.y){
            return 2;
        }else{
            return 1;
        }
    },

    //初始化地图移动范围
    initPositionerea:function(){

        this.maxH = this.tiledMap._mapSize.height;
        this.maxW = this.tiledMap._mapSize.width;
        
        var h = this.maxH*_MANAGERMAP.CELLSIZE*_CS.mapSize;
        var w = this.maxW*_MANAGERMAP.CELLSIZE*_CS.mapSize;
        var sh = cc.view.getVisibleSize().height;
        var sw = cc.view.getVisibleSize().width;

        if(h>sh){
            this.topy = h/2-sh/2;
            this.bomy = sh/2-h/2;
        }else{
            this.topy = 0;
            this.bomy = 0;
        }

        if(w>sw){
            this.leftx = sw/2-w/2;
            this.rightx = w/2-sw/2;
        }else{
            this.leftx = 0;
            this.rightx = 0;
        }

    },

    //获取计算小地图坐标的参数
    getParamForLittleMap:function(){

        var playerPos = this.playerNode.getPosition();
        var mapPos = _MANAGERMAP.getMapPos(playerPos.x,playerPos.y,this.maxH,this.maxW);

        return {
            x:mapPos.x,
            y:mapPos.y,
            width:this.maxW,
            height:this.maxH
        };
    },

    refreshInfoLabel:function(){

        this.refreshGoldInfo();
        this.refreshbars();
    },

    refreshbars:function(){

        var selectValueMap = _GAMEDATAHELPER.getSelectValue();
        this.levelBrightBar.progress = selectValueMap.xinyang/1000;
        this.levelDarkFightBar.progress = selectValueMap.gongli/1000;
        this.levelFightBar.progress = selectValueMap.ziyou/1000;
        this.darkTitleLabel.string = '功利:'+selectValueMap.gongli+'/1000';
        this.fightTitleLabel.string = '自由:'+selectValueMap.ziyou+'/1000';
        this.brightTitleLabel.string = '信仰:'+selectValueMap.xinyang+'/1000';

    },

    refreshGoldInfo:function(){
        //this.goldLabel.string = "金:"+_GAMEDATAHELPER.getMoney();
        //this.goldLabel.string = "金:"+_CU.getUnitNmb(_GAMEDATAHELPER.getMoney());
        //var playerPos = this.playerNode.getPosition();
        //var mapPos = _MANAGERMAP.getMapPos(playerPos.x,playerPos.y,this.maxH,this.maxW);
        //var mapPosStr = this.mapName+' x:'+mapPos.x+' y:'+mapPos.y;
        var npcconf = _NPC[window.gameData.playerTeam[0].npctype];
        this.hardLabel.string = _MCH.getHardName();
        this.goldLabel.string = "金:"+_CU.getUnitNmb(_GAMEDATAHELPER.getMoney());
        this.positionLabel.string = window.gameData.defaultMap;
        this.faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(npcconf.face);
                                    
    },

    refreshRoleInfo:function(){

        this.ishide = 0;

        for(var i=0;i<window.gameData.playerTeam.length;i++){

            var roleData = _ROLEDATA.getRoleDataOfPlayer(window.gameData.playerTeam[i]);
            if(roleData.hide){
                this.ishide = 1;
            }

        }

        if(this.ishide){
            this.hidelevelLabel.node.active = true;
        }else{
            this.hidelevelLabel.node.active = false;
        }

    },

    //判断是否身处可以存档的位置
    saveDataEnable:function(){
        return true;
    },

    //获取当前野区信息
    getCurrentFightInfo:function(){

        var playerMapPosition = this.getPlayerCurrentMapPosition();
        var pos = cc.v2(playerMapPosition.x,playerMapPosition.y);
        var armyArea = _MAA.getCurrentAreaArmy(pos);
        var areaType = this.getCurrentAreaType(pos);
        return {
            armyArea:armyArea,
            areaType:areaType
        };
    },

    //比怪标识
    openShengdun:function(){

        var flag = _GAMEDATAHELPER.getMissFightPer()?true:false;

        if(this.playerNode){
            this.playerNode.getChildByName('shendun').active = flag;
        }

        if(this.FirendNodeArr)
        for(var i=0;i<this.FirendNodeArr.length;i++){
            this.FirendNodeArr[i].getChildByName('shendun').active = flag;
        }

    },


    getNpcArrInMap:function(){

        var result = [];

        for(var key in this.npcMap){
            var posarr = key.split("-");
            var npclog = _GAMEDATAHELPER.getNpcLog(this.npcMap[key].t);
            if(
                npclog
                &&!npclog.die
                &&!npclog.zm
                &&!npclog.gy
                &&!npclog.diq
            ){
                result.push({npc:this.npcMap[key].t,x:posarr[0],y:posarr[1]});
            }
        }

        for(var key in this.jumpMap){

            var mapname = this.jumpMap[key].map;
            var mapconf = _MAPCONFIG[mapname];
            if(mapconf&&mapconf.master&&mapconf.master.length>0){

                var posarr = key.split("-");

                for(var i=0;i<mapconf.master.length;i++){

                    var npclog = _GAMEDATAHELPER.getNpcLog(mapconf.master[i]);
                    if(
                        npclog
                        &&!npclog.die
                        &&!npclog.zm
                        &&!npclog.gy
                        &&!npclog.diq
                    ){
                        result.push({npc:mapconf.master[i],x:posarr[0],y:posarr[1]});
                    }

                }

            }

        }

        return result;

    },

    //获取当前地图存在的npc
    getNpcInMap:function(){

        if(!_GAMEDATAHELPER.hasTeamSkill('yanli')){
            return null;
        }

        return this.getNpcArrInMap();
    },

    //刷新单个npc
    refreshOneNpcInMap:function(node,npc){
        var npcname = npc.npc;
        var x = npc.x;
        var y = npc.y;
        var npcconf = _NPC[npcname];
        var npclog = _GAMEDATAHELPER.getNpcLog(npcname);
        var imgnode = node.getChildByName('img');
        var namenode = node.getChildByName('name');
        var masknode = node.getChildByName('mask');
        var starnode = node.getChildByName('xinxin');

        if(npclog&&npclog.sign){
            imgnode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(npcconf.face);
            if(npclog.die){
                namenode.getComponent(cc.Label).string = npcname+"\n阵亡";
            }else{
                namenode.getComponent(cc.Label).string = npcname+"\nLv."+npcconf.lv;
            }
        }else{
            imgnode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame('问号');
            namenode.getComponent(cc.Label).string = "???";
        }

        if(_SKILLTEAMHELPER.isImportNpc(npcname)){
            starnode.active = true;
            starnode.getChildByName('message').getComponent(cc.Label).string='异';
        }else if(_SHOP[npcname]){
            starnode.active = true;
            if(_SHOP[npcname].type=="equip"){
                starnode.getChildByName('message').getComponent(cc.Label).string='装';
            }else if(_SHOP[npcname].type=="item"){
                starnode.getChildByName('message').getComponent(cc.Label).string='道';
            }else if(_SHOP[npcname].type=="book"){
                starnode.getChildByName('message').getComponent(cc.Label).string='书';
            }else{
                starnode.getChildByName('message').getComponent(cc.Label).string='?';
            }
        }else{
            starnode.active = false;
        }

        masknode.color = new cc.color(_CS.qua[npcconf.qua].color);
        node.npc = npc;
        node.on(cc.Node.EventType.TOUCH_START,function(){
            _MAP.moveToPointByXY(this.npc.x/1,this.npc.y/1);
        },
        node);
    },

    moveToPointByXY:function(x,y){
        var npcpos = _MANAGERMAP.getMapNodePosByTiled(x,y,_MAP.maxH,_MAP.maxW);
        _MAP.moveToPoint(npcpos,true);
    },

    showRename:function(){

        this.node.getChildByName('renameLabel').active = true;

        this.node.getChildByName('renameLabel').getChildByName('kuan')
        .getChildByName('inputBox')
        .getComponent(cc.EditBox).string=window.gameData.playerTeam[_PFM._ROLEINDEX].name;

        var repfRoleList = this.node.getChildByName('renameLabel')
                            .getChildByName('roleList').getChildren();

        for(var i=0;i<repfRoleList.length;i++){

            if(window.gameData.playerTeam[i]){
                repfRoleList[i].active = true;
                repfRoleList[i].getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(window.gameData.playerTeam[i].face);
            }else{
                repfRoleList[i].active = false;
            }

            if(_PFM._ROLEINDEX==i){
                repfRoleList[i].getChildByName('selected').active = true;
            }else{
                repfRoleList[i].getChildByName('selected').active = false;
            }

        }

    },

    closeRename:function(){
        this.node.getChildByName('renameLabel').getChildByName('kuan')
                                .getChildByName('inputBox')
                                .getComponent(cc.EditBox).string="";
        this.node.getChildByName('renameLabel').active = false;
    },

    realRename:function(name){
        if(!_NPC[name]){
            if(_GAMEDATAHELPER.reduceItemByName('改名卡')){
                if(window.gameData.playerTeam[_PFM._ROLEINDEX]){
                    _TIPS.showMessage( window.gameData.playerTeam[_PFM._ROLEINDEX].name+'改名为:'+name);
                    window.gameData.playerTeam[_PFM._ROLEINDEX].name = name;
                }
            }else{
                _TIPS.showMessage('改名卡不足修改失败');
            }
        }else{
            _TIPS.showMessage('不能与NPC同名');
        }
        //this.closeRename();
    },

    renameMainRole:function(){
        var newname = this.node.getChildByName('renameLabel').getChildByName('kuan')
                                .getChildByName('inputBox')
                                .getComponent(cc.EditBox).string;
        
        //console.log('新名称',newname);
        _HTTP.isMgc(newname,this);
    },

    //判定是否有ui悬浮
    canMove:function(){

        /*
        if(this.node.getChildByName('jifenLabel').active)
            return false;
        if(this.node.getChildByName('jiacen').active)
            return false;
        if(this.node.getChildByName('controller').active)
            return false;
        if(this.node.getChildByName('renameLabel').active)
            return false;
        if(this.node.getChildByName('repfLabel').active)
            return false;
        if(this.node.getChildByName('storyTips').active)
            return false;
        if(_PLAYERSTATUS.isOpen())
            return false;
        if(_TIPS.isOpen())
            return false;
        */

        for(var i=0;i<this.node.getChildren().length;i++){

            var tempnode = this.node.getChildren()[i];
            if(
                tempnode.getChildByName('mask')
                &&tempnode.active
            ){
                //console.log(tempnode,tempnode.getChildByName('mask'),tempnode.active);
                return false;
            }

        }

        return true;
    },

    showXQ:function(){
        _MG.initUi(this.node,{sence:_STYPE._XQSENCE,goodsType:'equipItem'});
    },

    showDz:function(){

        _MG.initUi(this.node,{sence:_STYPE._DZSENCE,goodsType:'equipItem'});
        
    },

    showCJB:function(){
        _MG.initUi(this.node,{sence:_STYPE._CJBSENCE,goodsType:'equipItem'});
    },

    update(){
        //this.refreshInfoLabel();
        /*
        var playerPosition = this.playerNode.getPosition();
        var tx = -playerPosition.x;
        var ty = -playerPosition.y
        var mapPosition = this.node.getChildByName('map').getPosition();

        if(tx<this.leftx||tx>this.rightx){
            tx = mapPosition.x;
        }

        if(ty<this.bomy||ty>this.topy){
            ty = mapPosition.y;
        }
        this.node.getChildByName('map').setPosition(tx,ty);*/
    },

    showTeachTask:function(){
        
    }

});
